package de.schule.barnimgymnasium.javafx.fxMediaPlayer;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.util.Duration;

public class FxMediaPlayer extends Application {

    private static final String MEDIA_URL = "file:///home/uwe/Downloads/oow2010-2.flv";

    @FXML
    volatile public MediaView mediaViewer;
    volatile public MediaPlayer mediaPlayer;
    volatile private File file;

    @Override
    public void start(final Stage primaryStage) throws Exception {
        // mediaPlayer.setAutoPlay(true);
        this.mediaViewer = new MediaView(this.mediaPlayer);
        final FXMLLoader fxmlLoader = new FXMLLoader(this.getClass().getResource("FxMediaPlayer.fxml"));
        fxmlLoader.setController(this);
        final Object root = fxmlLoader.load();
        if (root instanceof BorderPane) {
            final BorderPane bp = (BorderPane)root;
            bp.setCenter(this.mediaViewer);
            primaryStage.setScene(new Scene(bp));
        }
        primaryStage.setTitle("FxMediaPlayer");
        primaryStage.show();
    }

    @FXML
    protected void onButtonLoad(final ActionEvent event) {
        if (this.mediaPlayer != null) {
            this.mediaPlayer.stop();
        }
        final FileChooser fileChooser = new FileChooser();

        // Set extension filter
        fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Flash-Video (*.flv)", "*.flv"));
        fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("MP3-Audio (*.mp3)", "*.mp3"));
        fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("MPEG-4 (*.mp4)", "*.mp4"));
        fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("AVI files (*.avi)", "*.avi"));
        fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Matroska video (*.mkv)", "*.mkv"));
        // Show open file dialog
        this.file = fileChooser.showOpenDialog(null);
        if (this.file != null) {
            this.mediaPlayer = null;
            Media media;
            try {
                media = new Media(new URI("file", this.file.getCanonicalPath(), null).toString());
                // final Media media = new Media(FxMediaPlayer.MEDIA_URL);
                this.mediaPlayer = new MediaPlayer(media);
                this.mediaViewer.setMediaPlayer(this.mediaPlayer);
            } catch (URISyntaxException | IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    @FXML
    protected void onButtonPlay(final ActionEvent event) {
        if (this.mediaPlayer == null) {
            this.onButtonLoad(event);
        }
        if (this.mediaPlayer != null) {
            this.mediaPlayer.play();
        }
    }

    @FXML
    protected void onButtonPause(final ActionEvent event) {
        if (this.mediaPlayer != null) {
            this.mediaPlayer.pause();
        }
    }

    @FXML
    protected void onButtonRev(final ActionEvent event) {
        if (this.mediaPlayer != null) {
            this.mediaPlayer.pause();
            this.mediaPlayer.seek(this.mediaPlayer.getCurrentTime().subtract(new Duration(1000d / 30d)));
        }
    }

    @FXML
    protected void onButtonFwd(final ActionEvent event) {
        if (this.mediaPlayer != null) {
            this.mediaPlayer.pause();
            this.mediaPlayer.seek(this.mediaPlayer.getCurrentTime().add(new Duration(1000d / 30d)));
        }
    }

    protected void onRateChange() {
        if (this.mediaPlayer != null) {
            final double rate = 1;
            this.mediaPlayer.setRate(rate);
        }
    }

    /**
     * @param args
     */
    public static void main(final String[] args) {
        Application.launch(args);

    }
}
